﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FlashUnityPlayer
{
  public partial class Player : Control
  {
    public event EventHandler OnRightClick;

    public Player()
    {
      InitializeComponent();
      Application.AddMessageFilter(filter);
      filter.RightMouseDown += filter_RightMouseDown;
      filter.MouseMove += filter_MouseMove;
      
    }

    public enum PlayerSelect { Auto, Flash, Unity }
    public enum FlashUnityWindow { Fullscreen, Normal }
    private bool _IsFocused;
    FlashUnityMessageFilter filter = new FlashUnityMessageFilter();
    private Unity _unity3dPlayer;
    private Flash _flashPlayer;
    private String _VideoUrl = string.Empty;
    private PlayerSelect _PlayerMode = PlayerSelect.Auto;
    private FlashUnityWindow _PlayerWindow = FlashUnityWindow.Normal;

    public PlayerSelect PlayerMode
    {
      get { return _PlayerMode; }
      set { _PlayerMode = value; }
    }

    public FlashUnityWindow PlayerWindow
    {
      get { return _PlayerWindow; }
      set
      {
        _PlayerWindow = value;
        if (_PlayerWindow == FlashUnityWindow.Fullscreen)
        {
          this.Dock = DockStyle.Fill;
        }
        else
        {
          this.Dock = DockStyle.None;
          this.BringToFront();
        }
      }
    }

    public bool IsFocused
    {
      get { return _IsFocused; }
      set { _IsFocused = value; }
    }

    private void filter_MouseMove(object sender, MouseEventArgs e)
    {
      if (this.RectangleToScreen(this.ClientRectangle).Contains(e.Location))
        this.IsFocused = true;
      else this.IsFocused = false;
    }

    public void Play(string Url)
    {
      _VideoUrl = Url;
      LoadPlayer();
    }

    private void filter_RightMouseDown(object sender, MouseEventArgs e)
    {
      if (this.OnRightClick != null) this.OnRightClick(this, e);
    }

    private void LoadPlayer()
    {
      if (_VideoUrl.Contains(".unity3d") && PlayerMode == PlayerSelect.Auto || PlayerMode == PlayerSelect.Unity)
      {
        if (Unity.axHost == null)
        {
          _unity3dPlayer = new Unity(_VideoUrl);
          _unity3dPlayer.Dispose();
        }
        _unity3dPlayer = new Unity();
        this.Controls.Add(_unity3dPlayer);
        Unity.axHost = null;
      }
      else if (_VideoUrl.Contains(".swf") && PlayerMode == PlayerSelect.Auto || PlayerMode == PlayerSelect.Flash)
      {
        if (Flash.axHost == null)
        {
          try
          {
            _flashPlayer = new Flash(_VideoUrl);
            _flashPlayer.Dispose();
          }
          catch (Exception ex)
          {
            string e = ex.Message;
          }
        }
        _flashPlayer = new Flash(_VideoUrl);
        _flashPlayer.Dock = DockStyle.Fill;
        this.Controls.Add(_flashPlayer);
        Flash.axHost = null;
      }
    }
  }
}
